package moplayer;

import generation.methods.ComplexMarkov;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

public class ComplexMidiAnalyzer {

	public static HashMap<String, HashMap<Integer, Integer>> hashDHase = new HashMap<String, HashMap<Integer,Integer>>();
	public static Vector<Integer> vector = new Vector<Integer>();

	public static void fillMatrix(String previous, int next){
		if(!hashDHase.containsKey(previous)){
			hashDHase.put(previous, new HashMap<Integer, Integer>());
		}
		if(!hashDHase.get(previous).containsKey(next)){
			hashDHase.get(previous).put(next, 1);
		} else {
			int temp = hashDHase.get(previous).get(next);
			hashDHase.get(previous).put(next, temp + 1);
		}
	}
	
	public static int getIndex(int[] array, int number){
		int result = -1;
		for (int i = 0; i < array.length; i++) {
			if(array[i] == number){
				result = i;
			}
		}
		return result;
	}

	
	public static ComplexMarkov analyzeMIDI(String midiFile, String voice) {
		ComplexMarkov result = null;

		MidiLoader.loadMidi(midiFile, voice,vector);
		
		Set<Integer> notesUniverse = new HashSet<Integer>(vector);
		
		for (int i = 0; i < vector.size() - 2; i++) {
			Integer previous = vector.get(i);
			Integer previous2 = vector.get(i + 1);
			Integer next = vector.get(i + 2);
			fillMatrix(MoMidiUtils.indexize(previous, previous2), next);
		}
		
		Set<String> originNotesSet = hashDHase.keySet();
		int i = 0;
		int j = 0;
		for (String originNotes : originNotesSet) {
			//System.out.println("i = " + i);
			HashMap<Integer, Integer> sub = hashDHase.get(originNotes);
			Set<Integer> subset = sub.keySet();
			for (Integer targetNote : subset) {
				//System.out.println("j = " + j);
				Integer count = hashDHase.get(originNotes).get(targetNote);
				System.out.println("["+ originNotes +"]"+"["+ targetNote +"]"+" = " + count);
				j++;
				if(j == subset.size()){
					j = 0;
				}
			}
			i++;
			if(i == originNotesSet.size()){
				i = 0;
			}
		}
		
		double[] soma = new double[originNotesSet.size()];
		i = 0;
		j = 0;
		for (String originNote : originNotesSet){
			HashMap<Integer, Integer> sub = hashDHase.get(originNote);
			Set<Integer> subset = sub.keySet();
			for (Integer next : subset) {
				Integer count = hashDHase.get(originNote).get(next);
				soma[i] = soma[i] + count;
			}
			i++;
		}
		
		System.out.println();
		int[] notasX = new int[notesUniverse.size()];
		i = 0;
		for (int note : notesUniverse) {
			notasX[i] = note;
			i++;
		}
		String[] originNotesSetArray = new String[originNotesSet.size()];
		i = 0;
		for (String string : originNotesSet) {
			originNotesSetArray[i] = string;
			i++;
		}

		double[][] markovMatrix = new double[originNotesSet.size()][notesUniverse.size()];
		// Andando de novo:
		i = 0;
		for (String originNote : originNotesSet){
			HashMap<Integer, Integer> sub = hashDHase.get(originNote);
			Set<Integer> subset = sub.keySet();
			for (Integer next : subset) {
				Integer count = hashDHase.get(originNote).get(next);
				//int index1 = getIndex(notasX, prev);
				//int index2 = getIndex(notasX, next);
				markovMatrix[i][MoMidiUtils.getIndex(notasX, next)] = count/soma[i];
			}
			i++;
		}

		
		
		result = new ComplexMarkov(notasX, originNotesSetArray, markovMatrix);
		return result;
	}
	
	

	public static void main(String[] args) {
		
		analyzeMIDI("midis/meu.mid", "0");
	
	}
	
	

}
